#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', {
    compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')),
    gradle_tarfile => c("input_files_by_name/gradle"),
    ndk_version => c("var/ndk_version"),
  }) %]
distdir=/var/tmp/dist/[% project %]
builddir=/var/tmp/build/[% project %]
mkdir $distdir
mkdir /var/tmp/build

[% IF c("input_files_by_name/android_ndk") -%]
  pushd $(dirname $ANDROID_NDK_HOME)
  unzip -qq $rootdir/[% c("input_files_by_name/android_ndk") %]
  # Without this link gradle will not find the NDK.
  ln -s $(basename $ANDROID_NDK_HOME) [% c("var/ndk_version_build") %]
  popd
[% END -%]

cd /var/tmp/dist
tar -xf $rootdir/[% c('input_files_by_name/rust') %]
tar -xf $rootdir/[% c('input_files_by_name/ninja') %]
export PATH=/var/tmp/dist/rust/bin:/var/tmp/dist/ninja:$PATH
export RUST_ANDROID_GRADLE_PYTHON_COMMAND=python3
cd $rootdir

export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64

[% IF !c('var/fetch_gradle_dependencies') %]
  gradle_repo=/var/tmp/dist/gradle-dependencies
  mv $rootdir/[% c('input_files_by_name/gradle-dependencies') %] $gradle_repo
  cp -rl $gradle_repo/dl/android/maven2/* $gradle_repo || true
  cp -rl $gradle_repo/m2/* $gradle_repo || true
  cp -rl $gradle_repo/maven2/* $gradle_repo || true
[% END %]

tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.[% c('compress_tar') %]

# Prepare the offline build.
# Move the directory for hardcoding the path in .cargo/config.
mv /var/tmp/build/[% project %]-[% c('version') %] $builddir
tar -C $builddir -xf $rootdir/[% c('input_files_by_name/cargo_vendor') %]

cat > pip.conf << 'EOF'
[global]
find-links = /var/tmp/build/application-services/glean-wheels
index-url =
no-index = yes
EOF
export PIP_CONFIG_FILE=$rootdir/pip.conf

cd $builddir
# Make sure our vendored crates are used for offline builds.
cat >> .cargo/config << 'EOF'

[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "/var/tmp/build/application-services/vendor"

[net]
offline=true
EOF

patch -p1 < $rootdir/no-git.patch
patch -p1 < $rootdir/bug40485.diff

pushd libs
mv $rootdir/[% c("input_files_by_name/nss") %] ./
mv $rootdir/[% c("input_files_by_name/sqlcipher") %] ./
mv $rootdir/bug_13028.patch ./
patch -p2 < $rootdir/apply-bug-13028.diff
sed -i 's/NDK_VERSION=.*/NDK_VERSION=[% c("var/ndk_version_build") %]/g' android_defaults.sh
./build-all.sh desktop
./build-all.sh android
popd

[% IF c('var/fetch_gradle_dependencies') %]
  # XXX: `assemble` is still not enough to see all fetched dependencies via
  # Gradle's --debug. See: tor-browser-build#40056.
  gradle --debug --no-daemon assemble > gradle.log 2>&1 || ( cat gradle.log; exit 1 )
  $rootdir/gen_gradle_deps_file.sh gradle.log
  mv gradle-dependencies-list.txt '[% dest_dir _ "/" _ c("filename") %]'
[% ELSE %]
  # Prepare Glean dependencies for offline build
  tar -xf $rootdir/[% c('input_files_by_name/glean-wheels') %]
  # We need to set `LC_ALL` and `LANG` to something that is not ASCII as encoding
  # otherwise `click` barfs. See: https://click.palletsprojects.com/python3/
  export LC_ALL=C.UTF-8
  export LANG=C.UTF-8
  patch -p1 < $rootdir/local-repository.diff
  gradle_flags="--offline --no-daemon"
  gradle $gradle_flags assembleRelease
  gradle $gradle_flags publish
  mv build/maven $distdir

  pushd components/support/nimbus-fml
  cargo build --release
  popd
  cp target/release/nimbus-fml $distdir

  cd /var/tmp/dist
  [% c('tar', {
      tar_src => [ project ],
      tar_args => '-caf ' _ dest_dir _ '/' _ c('filename'),
  }) %]
[% END %]
